package mi.payment.utils.datacollection;

import android.content.Intent;

import com.google.gson.Gson;
import com.sunmi.statistics.library.DataBean;
import com.sunmi.statistics.library.MataDataConstant;
import com.sunmi.statistics.library.StatisticsManager;

import java.util.ArrayList;
import java.util.List;

import mi.payment.BuildConfig;
import mi.payment.MyApplication;
import mi.payment.bean.datacollection.DataServiceInfo;
import mi.payment.bean.datacollection.PayInfo;
import mi.payment.db.dao.PayDetail;
import sunmi.sunmiui.utils.LogUtil;

/**
 * Created by zjr on 2018/1/22.
 * address :zhoujirui@sunmi.com
 * 数据收集时 支付明细工具类
 */

public class PayDetailCollection {
    private static String TAG = "PayDetailCollection";
    private static final String TYPE = "payment_app_item_info";//业务类型：支付明细
    private List<DataBean> list = new ArrayList<>();
    private boolean isCorrectTransaction = false;//是否是冲正交易
    private boolean isSign = false;//是否是签到
    private boolean isSignOut = false;//是否是签退

    private static final String ACTION_DATA_COLLECTION = "dataservice.action.data_collection";//旧版本dataService广播
    private static final String ACTION_FLAG = "DataService";//dataService广播接收数据字段

    private static final class SingletonHolder {
        private static final PayDetailCollection INSTANCE = new PayDetailCollection();
    }

    private PayDetailCollection() {

    }

    public static PayDetailCollection getInstance() {
        return SingletonHolder.INSTANCE;
    }

//    //添加每次的支付信息
//    public void addData(PayDetail payDetail) {
//        PayInfo payContent = new PayInfo(payDetail);
//        payInfoList.add(payContent);
//    }

    //结算完成时上送数据
    public void uploadData(PayDetail payDetail) {
        if (payDetail == null) {
            LogUtil.e(TAG, "payDetail is null");
            return;
        }
        PayInfo payInfo = new PayInfo(payDetail);
        if (isCorrectTransaction()) {//是否是冲正交易
            payInfo.setTradeType(12);
            setCorrectTransaction(false);
        }
        if (isSign()) {//是否是签到
            payInfo.setTradeType(9);
            setSign(false);
        }
        if (isSignOut()) {//是否是签退
            payInfo.setTradeType(10);
            setSignOut(false);
        }

        if (BuildConfig.BUILD_TYPE.equals("futureDebug") || BuildConfig.BUILD_TYPE.equals("futureRelease")) {//新版本dataService
            sendMessage(payInfo);
        } else {//旧版本dataService
            sendBroadcast(payInfo);
        }
    }

    //连接旧版本dataService
    private void sendBroadcast(PayInfo payInfo) {
        Intent intent = new Intent();
        intent.setAction(ACTION_DATA_COLLECTION);
        DataServiceInfo dataServiceInfo = new DataServiceInfo(TYPE, payInfo, payInfo.getTime());
        Gson gson = new Gson();
        String jsonString = gson.toJson(dataServiceInfo);
        LogUtil.e(TAG, "jsonString = " + jsonString);
        intent.putExtra(ACTION_FLAG, jsonString);
        MyApplication.getInstance().sendBroadcast(intent);
    }

    //连接新版本dataService
    private void sendMessage(PayInfo payInfo) {
        long id = 0;//该id随便设置，dataService不做处理
        long time = payInfo.getTime();//本地交易时间戳
        String type = MataDataConstant.TYPE_INFO;//明细
        String packageName = MyApplication.getInstance().getPackageName();//包名
        String businessType = TYPE;//业务类型
        int priorityType = MataDataConstant.MIDDLE_PRIORITY;//中级
        int flowType = MataDataConstant.FLOW_DIFFICULT;//最低支持省流量模式
        int updateType = MataDataConstant.TYPE_SCHEDULE;//定时上报
        Gson gson = new Gson();
        String content = gson.toJson(payInfo);//采集的数据 JSONArray
        LogUtil.e(TAG, "jsonString ->content = " + content);
        DataBean dataBean = new DataBean(id, time, type, packageName, businessType, priorityType, flowType, updateType, content);
        list.add(dataBean);
        StatisticsManager.getInstance().addList(MyApplication.getInstance(), list);//上报数据
        clearData();
    }

    public void clearData() {
        list.clear();
    }

    public boolean isCorrectTransaction() {
        return isCorrectTransaction;
    }

    public boolean isSign() {
        return isSign;
    }

    public boolean isSignOut() {
        return isSignOut;
    }

    //设置冲正标志
    public void setCorrectTransaction(boolean correctTransaction) {
        isCorrectTransaction = correctTransaction;
    }

    //设置签到标志
    public void setSign(boolean sign) {
        isSign = sign;
    }

    //设置签退标志
    public void setSignOut(boolean signOut) {
        isSignOut = signOut;
    }
}
